<?php

namespace App\Http\Controllers\lxSystem;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\Brand;
use App\Models\Category;
use App\Models\ProductGrallery;
use Illuminate\Database\Eloquent\Collection;

class ProductController extends Controller
{

    //123123123
    private function attributes()
    {
        view()->share([
            'categories' => Category::get_categories(),
            'brands' => Brand::orderBy('sort_order')->get(),

        ]);
    }

    function index(Request $request)
    {
        $where = function ($query) use ($request) {
            if ($request->has('category_id') and $request->category_id != '') {
                $category_id = $request->category_id;
                $product_ids = \DB::table('category_product')->where('category_id', $category_id)->pluck('product_id');
                $query->whereIn('id', $product_ids);
            }
            if ($request->has('name') and $request->name != '') {
                $search = "%" . $request->name . "%";
                $query->where('name', 'like', $search);
            }
            
            if ($request->has('brand_id') and $request->brand_id != '-1') {
                $query->where('brand_id', $request->brand_id);
            }

            if ($request->has('is_onsale') and $request->is_onsale != '-1') {
                $query->where('is_onsale', $request->is_onsale);
            }

            if ($request->has('is_top')) {
                $query->where('is_top', true);
            }

            if ($request->has('is_recommend')) {
                $query->where('is_recommend', true);
            }

            if ($request->has('is_hot')) {
                $query->where('is_hot', true);
            }

            if ($request->has('is_new')) {
                $query->where('is_new', true);
            }

            if ($request->has('created_at') and $request->created_at != '') {
                $time = explode(" ~ ", $request->input('created_at'));
                foreach ($time as $k => $v) {
                    $time["$k"] = $k == 0 ? $v . " 00:00:00" : $v . " 23:59:59";
                }
                $query->whereBetween('created_at', $time);
            }
        };
        $products = Product::with(['categories'=>function($query){
            $query->select('name');
        }])->with('brand')->where($where)->paginate(5);
        $this->attributes();
        return view('lxSystem.products.index')->with(['products' => $products]);
    }

    function create()
    {
        $this->attributes();
        return view('lxSystem.products.create');
    }

    function store(Request $request)
    {
        $product = Product::create(collect($request)->forget(['category_id','imgs','file'])->all());
        if($request->has('imgs')){
            foreach($request->imgs as $img){
                $product->product_gralleries()->create(['img' => $img]);
            }
        }

        $product->categories()->sync($request->category_id);
        return redirect(route('products.index'));
    }

    function edit($id)
    {

        $product = Product::with('categories')->with('brand')->with('product_gralleries')->find($id);

        //$product = Product::with('categories')->with('brand')->find($id);

        foreach ($product->categories as $c) {
            $ca[] = $c->id;
        }
        $this->attributes();
        return view('lxSystem.products.edit')->with(['product' => $product, 'pcategory' => $ca]);
    }

    function update(Request $request, $id)
    {
        $product = Product::find($id);
        $product->categories()->sync($request->category_id);

        $product->update(collect($request)->forget(['category_id','imgs','file'])->all());

        if($request->has('imgs')){
            foreach($request->imgs as $img){
                $product->product_gralleries()->create(['img' => $img]);
            }
        }
        return redirect(route('products.index'));
    }

    function destroy($id)
    {
        Product::destroy($id);
        return redirect(route('products.index'));
    }

    function gallery_delete(Request $request){
        ProductGrallery::destroy($request->id);
    }

    function destroy_checked(Request $request)
    {
        $delete_id[] = $request->checked_id;
        foreach ($delete_id as $id) {
            Product::destroy($id);
        }
    }

    function change_stock(Request $request)
    {
        $product = Product::find($request->id);
        $product->stock = $request->stock;
        $product->save();
    }

    function is_something(Request $request)
    {
        $attr = $request->attr;
        $product = Product::find($request->id);
        $value = $product->$attr ? false : true;
        $product->$attr = $value;
        $product->save();
    }

    public function trash()
    {
        $products = Product::with('categories')->with('brand')->with('product_gralleries')->
        onlyTrashed()->paginate(5);
        return view('lxSystem.products.trash')->with(['products'=>$products]);
    }

    public function restore($id){
        Product::withTrashed()->where('id', $id)->restore();
        return back()->with('info', '恢复成功');
    }

    public function force_destroy($id)
    {
        $product = Product::onlyTrashed()->find($id);
        $product->categories()->sync([]);
        Product::withTrashed()->where('id', $id)->forceDelete();
        ProductGrallery::where('product_id',$id)->delete();

        return back()->with('info', '删除成功');
    }


    public function restore_checked(Request $request)
    {
        $checked_id = $request->checked_id;
        Product::withTrashed()->whereIn('id', $checked_id)->restore();
        return back()->with('info', '还原成功');
    }
    function force_destroy_checked(Request $request)
    {
        $checked_id = $request->checked_id;
        foreach($checked_id as $id){
            $product = Product::onlyTrashed()->find($id);
            $product->categories()->sync([]);
        }
        ProductGrallery::whereIn('product_id', $checked_id)->delete();
        Product::withTrashed()->whereIn('id', $checked_id)->forceDelete();
    }
}
